package iterator;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 0171093 on 2018/3/16.
 */
public class NewChineseBooks implements Container {

    private List<Book> mBooks;

    private static NewChineseBooks sChineseBooks;

    public static NewChineseBooks getInstance() {
        if (sChineseBooks == null) {
            sChineseBooks = new NewChineseBooks();
        }
        return sChineseBooks;
    }

    private NewChineseBooks() {
        mBooks = new ArrayList<>();
        mBooks.add(new Book("论语"));
        mBooks.add(new Book("诗经"));
        mBooks.add(new Book("唐诗"));
        mBooks.add(new Book("宋词"));
        mBooks.add(new Book("明清小说"));
        mBooks.add(new Book("近代散文"));
    }

    @Override
    public BookIterator getIterator() {

        return new ChineseBookIterator();
    }

    @Override
    public BookIterator getReverseIterator() {
        return new ChineseReverseInterator();
    }


    //正序迭代器
    public class ChineseBookIterator implements BookIterator {
        int mIndex = 0;

        @Override
        public boolean hasNext() {
            return mBooks.size() > mIndex;
        }

        @Override
        public Object next() {
            return mBooks.get(mIndex++);
        }
    }

    //倒序迭代器
    public class ChineseReverseInterator implements BookIterator {
        int mIndex = mBooks.size() - 1;

        @Override
        public boolean hasNext() {
            return mIndex >= 0;
        }

        @Override
        public Object next() {
            return mBooks.get(mIndex--);
        }
    }


}
